#include "userauth.hpp"

void userauth::userreg(name admin, name sname, string password)
{
    require_auth(admin);
    users us(get_self(), admin.value);

    auto itr = us.find(sname.value);
    //判断用户是否存在
    check(itr == us.end(), "user already exists");

    //新增数据
    us.emplace(admin, [&](auto &g){
        g.username = sname;
        g.password = password; // 不安全，去搜一下哈希函数
    });
    

}

string userauth::login(name admin, name sname, string password)
{
    require_auth(admin);
    users us(get_self(), admin.value);

    auto itr = us.find(sname.value);
    //判断用户是否存在
    check(itr != us.end(), "user does not exists");

    if(itr->password == password) {
        return "true";
    }

    return "false";
}